
/**
 * Created by wuye on 2017/12/11.
 */
$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/interview/list',
        datatype: "json",
        colModel: [{
                label: 'id',
                name: 'id',
                index: "id",
                width: 20,
                key: true
            },
           /* {
                label: '编号',
                name: 'bookno',
                index: "bookno",
                width: 60
            },*/
            {
                label: '标题',
                name: 'headline',
                width: 55
            },
            {
                label: '内容',
                name: 'content',
                width: 75
            },
            {
                label: '封面',
                name: 'picture',
                width: 75
            },
            {
                label: '创建时间',
                name: 'date',
                width: 40
            },
            {
                label: '点击数',
                name: 'clickCount',
                width: 20
            },
            {
                label: '评论数',
                name: 'commentCount',
                width: 20
            },
            {
                label: '点赞数',
                name: 'likeCount',
                width: 20
            }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.records",
            page: "page.current",
            total: "page.pages",
            records: "page.total"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({
                "overflow-x": "hidden"
            });
        }
    });
});

var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url: "nourl"
        }
    }
};
var ztree;

var vm = new Vue({
    el: '#rrapp',
    data: {
        // 显示添加章节的dom节点
        showDom: 'list',
        q: {
            headline: null,


        },
        showList: true,
        title: null,
        interview: {
            headline: null,
            content: null,
            date: null,
            picture: null,
            clickCount: null,
            commentCount: null,
            likeCount: null
        },

        interview2: {
            headline: null,
            content: null,
            date: null,
            picture: null,
            clickCount: null,
            commentCount: null,
            likeCount: null

        },
        interviewInfo: {
            spokesman: null,
            x: null,
            z: null,
        },
        sels: [
            // {value:"1",week:"一"},
            // {value:"2",week:"二"},
            // {value:"3",week:"三"},
            // {value:"4",week:"四"}
        ]
    },
    methods: {
        getInterviewInfoList: function () {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showDom = "interviewInfoList"
            window.location.href = 'interviewInfo.html?id='+id
        },
        changeImage: function (e) {
            var file = e.target.files[0]
            var reader = new FileReader()
            var THIS = this
            reader.readAsDataURL(file)
            reader.onload = function (e) {
                // this.result 为图片
                THIS.upload(this.result)
            }
        },
        upload: function ($refs) {
            // 上传图片
            var THIS = this;
            if (this.$refs.file.files.length !== 0) {
                var image = new FormData()
                image.append('file', this.$refs.file.files[0])

                $.ajax({
                    url: '/youxiang/api/upload/up',
                    data: image,
                    // headers: {
                    //     "Content-Type": "multipart/form-data"
                    // },
                    cache: false,
                    processData: false,
                    contentType: false,
                    type: 'post',
                    success: function (result) {
                        console.log(result);
                        THIS.interview.picture = result.url
                    },
                    error: function (error) {
                        alert('图片上传失败!');
                    }
                })
            }
        },
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.interview.headline= null,
            vm.interview.content= null,
            vm.interview.date= null,
            vm.interview.clickCount= null,
            vm.interview.picture= null,
            vm.interview.likeCount= null,
            vm.interview.commentCount = null,
            vm.showList = false;
            vm.showDom = 'addOrEdit';
            vm.title = "添加访谈";
        },
        update: function () {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.showDom = 'addOrEdit'

            vm.title = "修改访谈";
            vm.getInterivew(id);
        },
        del: function () {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }
            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/interview/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function () {
                                vm.reload();
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        saveOrUpdate: function () {
            var url = "/sys/interview/saveOrUpdate"
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.interview),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function () {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        getInterivew: function (id) {
            $.get(baseURL + "sys/interview/findById/" + id, function (r) {
                vm.interview = r.interview;
            });
        },
        reload: function () {
            vm.showList = true;
            vm.showDom = 'list'

            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    'headline': vm.q.headline
                },
                page: page
            }).trigger("reloadGrid");
        }
    }
});